package com.lzw.breezeNovel.webApp.service.impl.video;

import com.lzw.breezeNovel.common.exception.BaseException;
import com.lzw.breezeNovel.common.login.LoginUser;
import com.lzw.breezeNovel.common.login.LoginUserHolder;
import com.lzw.breezeNovel.common.result.ResultCodeEnum;
import com.lzw.breezeNovel.model.entity.pojo.user.UserInfo;
import com.lzw.breezeNovel.model.entity.pojo.video.VideoComment;
import com.lzw.breezeNovel.model.entity.query.PaginationResultQuery;
import com.lzw.breezeNovel.model.entity.query.video.CommnetQuery;
import com.lzw.breezeNovel.webApp.mapper.user.UserInfoMapper;
import com.lzw.breezeNovel.webApp.mapper.video.VideoCommentMapper;
import com.lzw.breezeNovel.webApp.service.video.VideoCommentService;
import com.lzw.breezeNovel.webApp.vo.video.VideoCommentVo;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author 清风明月
 * @Package com.lzw.breezeNovel.webApp.service.impl.video
 * @date 2025/3/19
 * @description 评论
 */
@Service
@SuppressWarnings("all")
public class VideoCommentServiceImpl implements VideoCommentService {

    /**
     * 注入mapper
     */
    @Autowired
    private VideoCommentMapper videoCommentMapper;
    /**
     * 注入用户mapper
     */
    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 评论列表
     * @param commnetQuery
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public List<VideoCommentVo> list(@NotNull CommnetQuery commnetQuery) {
        //获取当前用户
        Long userId = getCurrentUser();
        //计算分页
        int pageNo = startPage(commnetQuery.getPageNo(), commnetQuery.getPageSize());
        commnetQuery.setPageNo(pageNo);
        //查询评论列表
        List<VideoComment> videoCommentVos = videoCommentMapper.list(userId, commnetQuery);
        //进行拷贝
        List<VideoCommentVo> videoCommentVoList = Copy(videoCommentVos);
        return videoCommentVoList;
    }

    /**
     * 添加评论
     *
     * @param type
     * @param content
     * @param videoId
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(Integer type, String content, Long videoId) {
        //获取当前用户
        Long userId = getCurrentUser();
        //创建评论对象
        VideoComment videoComment = new VideoComment();
        //设置评论内容
        videoComment.setContent(content);
        //设置评论类型
        videoComment.setType(type);
        //设置评论用户
        videoComment.setUserId(userId);
        //设置评论视频
        videoComment.setId(videoId);
        //实在是否被观看
        videoComment.setIsWatch(0);
        //保存评论
        videoCommentMapper.insert(videoComment);

    }

    /**
     * 根据用户id获取评论
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public PaginationResultQuery<VideoCommentVo> listByUserId(Integer pageNo, Integer size) {
        //获取当前用户
        Long userId = getCurrentUser();
        //计算分页
        int startPage = startPage(pageNo, size);
        //分页查询评论列表
        List<VideoComment> videoCommentVos = videoCommentMapper.listByUserId(userId, startPage, size);
        //拷贝数据
        List<VideoCommentVo> videoCommentVoList = Copy(videoCommentVos);
       //分装分页对象
        PaginationResultQuery<VideoCommentVo> paginationResultQuery = new PaginationResultQuery<>(0,size,startPage,videoCommentVoList);
        return paginationResultQuery;
    }

    /**
     * 删除评论
     * @param commentId
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long commentId) {
        videoCommentMapper.updateComment(commentId);
    }

    /**
     * 拷贝
     * @param videoCommentVos
     * @return
     */
    private List<VideoCommentVo> Copy(List<VideoComment> videoCommentVos) {
        //判断是否为空
        if (videoCommentVos == null || videoCommentVos.size() == 0) {
            return null;
        }
        //拷贝数据
        return videoCommentVos.stream().map(videoComment -> {
            VideoCommentVo videoCommentVo = new VideoCommentVo();
            BeanUtils.copyProperties(videoComment, videoCommentVo);
            //获取评论时间
            Date commentTime = videoComment.getCommentTime();
            // 格式化时间
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String formattedTime = dateFormat.format(commentTime);
            // 将格式化后的时间赋值给 VideoCommentVo
            videoCommentVo.setCommentTime(formattedTime);
            return videoCommentVo;
        }).collect(Collectors.toList());
    }


    /**
     * 获取当前用户
     */
    private Long getCurrentUser(){
        //获取当前用户
        LoginUser loginUser = LoginUserHolder.getLoginUser();
        //判断是否存在
        if(loginUser == null)
        {
            throw new BaseException(ResultCodeEnum.APP_LOGIN_AUTH);
        }
        //获取手机号
        String phone = loginUser.getPhone();
        //获取用户信息
        UserInfo userInfo = userInfoMapper.selectByPhone(phone);
        return userInfo.getUserId();
    }

    /**
     * 计算分页
     * @param pageNo
     * @param size
     * @return
     */
    public int startPage(Integer pageNo, Integer size){
        //判断分页数据是否为空
        pageNo = Optional.of(pageNo).orElse(1);
        size = Optional.of(size).orElse(10);
        pageNo = Math.max(pageNo, 1);
        size = Math.max(size, 1);
        //计算分页
        pageNo = (pageNo - 1) * size;
        return pageNo;
    }
}
